<?php

class Model_FriendsMapper
{
    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Model_DbTable_Friends');
        }
        return $this->_dbTable;
    }
	
    //insert or update data into friends table
    public function save(Model_Friends $friends)
    {
    	
        $data = array(
        	'founder_id' => $friends->getFounder_id(),
        	'founder_name' => $friends->getFounder_name(),
        	'target_id' => $friends->getTarget_id(),
        	'target_name' => $friends->getTarget_name(),
        	'statues' => $friends->getStatues(),
        	'request_message' => $friends->getRequest_message(),
        );

        if (null === ($id = $friends->getFriend_id())) {
        	// if no id input, then generate it and insert the data
            $this->getDbTable()->insert($data);
        } else {
        	// if an id input, then update the data which match this id
            $this->getDbTable()->update($data, array('friend_id = ?' => $id));
        }
    }
    
    //check if the friends data existed.
    public function chechUnique($founderId, $targetId) {
    	
		$select = $this->getDbTable()->select();
		$select->from('friends',array('friend_id'))
					 ->where('statues!=?','J')
					 ->where('founder_id=?', $founderId)
					 ->where('target_id=?', $targetId);
		$result = $this->getDbTable()->fetchRow($select);
		if($result){
			return true;
		}
		return false;
    }
    
    // select data from friends table
    public function find($friendsId, Model_Friends $friends)
    {
        $result = $this->getDbTable()->find($friendsId);
        
		if (0 == count($result)) {
            return;
        }
        $row = $result->current();
		$friends->setFriend_id($row->friend_id);
		$friends->setFounder_id($row->founder_id);
		$friends->setFounder_name($row->founder_name);
		$friends->setTarget_id($row->target_id);
		$friends->setTarget_name($row->target_name);
		$friends->setStatues($row->statues);
		$friends->setRequest_message($row->request_message);
    }
    
    //find all friends by user id which is confirmed
    public function findFriendsListByUserid($userId){
    	
    	$usersMapper = new Model_UsersMapper();
    	$users = array();
    	//select the records which user id is equal to fouder or target and confirmed
    	$select = $this->getDbTable()->select();
    	$select->from('friends')
					 ->where('statues=?', 'C')
					 ->where('founder_id=?',$userId)
					 ->orWhere('target_id=?',$userId);	
					 
		$friedns = $this->findAllBySelector($select);
		return $friedns;
    }
    
    //find the friends list which are waiting for being confirm.
    public function findUnconfirmListByUserId($userId){
    	
    	$usersMapper = new Model_UsersMapper();
    	$users = array();
    	//select the records which user id is equal to target and unconfirmed
    	$select = $this->getDbTable()->select();
    	$select->from('friends')
					 ->where('target_id=?', $userId)
					 ->where('statues=?','W');
		$friends = $this->findAllBySelector($select);
		return $friends;
    }
    
    protected function findAllBySelector($selector){
    	
    	$results = $this->getDbTable()->fetchAll($selector);
    	$friends = array();
    	
    	if(0==count($results)){
    		return $friends;
    	}
    	
    	foreach ($results as $row){
    		$row = $results->current();
    		$friend = new Model_Friends();
    		$friend->setFriend_id($row->friend_id);
			$friend->setFounder_id($row->founder_id);
			$friend->setFounder_name($row->founder_name);
			$friend->setTarget_id($row->target_id);
			$friend->setTarget_name($row->target_name);
			$friend->setStatues($row->statues);
			$friend->setRequest_message($row->request_message);
			$friends[] = $friend;
    	}
    	return $friends;
    }
}

